package com.zzyl.nursing.controller;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.nursing.dto.HealthAssessmentDto;
import com.zzyl.oss.AliyunOSSOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.HealthAssessment;
import com.zzyl.nursing.service.IHealthAssessmentService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.zzyl.common.utils.PDFUtil;
/**
 * 健康评估Controller
 * 
 * @author lww
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/nursing/healthAssessment")
@Api(tags = "健康评估相关接口")
public class HealthAssessmentController extends BaseController
{
    @Autowired
    private IHealthAssessmentService healthAssessmentService;


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    /**
     * 查询健康评估列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取健康评估列表")
    public TableDataInfo<List<HealthAssessment>> list(HealthAssessment healthAssessment)
    {
        startPage();
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        return getDataTable(list);
    }

    /**
     * 导出健康评估列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:export')")
    @Log(title = "健康评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出健康评估列表")
    public void export(HttpServletResponse response, HealthAssessment healthAssessment)
    {
        List<HealthAssessment> list = healthAssessmentService.selectHealthAssessmentList(healthAssessment);
        ExcelUtil<HealthAssessment> util = new ExcelUtil<HealthAssessment>(HealthAssessment.class);
        util.exportExcel(response, list, "健康评估数据");
    }

    /**
     * 获取健康评估详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取健康评估详细信息")
    public R<HealthAssessment> getInfo(@ApiParam(value = "健康评估ID", required = true)
            @PathVariable("id") Long id)
    {
        return R.ok(healthAssessmentService.selectHealthAssessmentById(id));
    }

    /**
     * 新增健康评估
     */
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:add')")
    @Log(title = "健康评估", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增健康评估")
    public AjaxResult add(@ApiParam(value = "健康评估实体", required = true) @RequestBody HealthAssessmentDto healthAssessmentDto)
    {
        Long id = healthAssessmentService.insertHealthAssessment(healthAssessmentDto);
        return AjaxResult.success(id);
    }

    /**
     * 修改健康评估
     */
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:edit')")
    @Log(title = "健康评估", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改健康评估")
    public AjaxResult edit(@ApiParam(value = "健康评估实体", required = true) @RequestBody HealthAssessment healthAssessment)
    {
        return toAjax(healthAssessmentService.updateHealthAssessment(healthAssessment));
    }

    /**
     * 删除健康评估
     */
    @PreAuthorize("@ss.hasPermi('nursing:healthAssessment:remove')")
    @Log(title = "健康评估", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除健康评估")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
    {
        return toAjax(healthAssessmentService.deleteHealthAssessmentByIds(ids));
    }


    @PostMapping("/upload")
    @ApiOperation("健康文档上传")
    public AjaxResult uploadFile(MultipartFile file,String idCardNo) throws Exception{

//
//        //上传到oss
//        String upload = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
//
//        //将内容读取出来
//        String pdfText = PDFUtil.getPDFText(file.getInputStream());
//        //放入redis中
//        redisCache.setCacheMapValue("healthReport",idCardNo,pdfText);
//
//        //返回ajax 返回结果
//        AjaxResult success = AjaxResult.success();
//        success.put("fileName", upload);
//        success.put("originalFilename", file.getOriginalFilename());
//        success.put("newFileName",upload.substring(upload.lastIndexOf("/")+1));
//        success.put("url", upload);
        //上传体检报告
        //字符流只能使用一次要不然会报错
//        byte[] fileBytes = file.getBytes();
//        //存储到oss
//        String url = aliyunOSSOperator.upload(fileBytes, Objects.requireNonNull(file.getOriginalFilename()));
//        //读取pdf内容，存到redis中
//
//        String pdfText = PDFUtil.getPDFText(new ByteArrayInputStream(fileBytes));
//        redisCache.setCacheMapValue("healthreport", idCardNo, pdfText);
//        //封装ajax返回结果
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("fileName", url);
//        ajax.put("newFileName", url.substring(url.lastIndexOf("/")+1));
//        ajax.put("originalFilename", file.getOriginalFilename());

        //如果硬要将部分代码放入业务层，则需要将file.getInputStream()传进去.会乱码直接穿的话
        try {
            // 上传到OSS
            byte[] bytes = file.getBytes();
            String url = aliyunOSSOperator.upload(bytes, file.getOriginalFilename());

            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", url);
            // ajax.put("newFileName", url.substring(url.lastIndexOf("/")));
            ajax.put("originalFilename", file.getOriginalFilename());
            // PDF文件内容读取为字符串
            String content = PDFUtil.getPDFText(new ByteArrayInputStream(bytes));
            // 临时存储到redis中
//            redisCache.opsForHash().put("healthReport", idCardNo, content);
            //分类放好，大key为healthreport，小key为身份证号
            redisCache.setCacheMapValue("healthReport", idCardNo, content);
            return ajax;
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }
}
